package dev.xesam.bottomsheet;


import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import dev.xesam.bottomsheet.adapter.MenuAdapter;

/**
 * BottomSheet
 */
public class BottomSheet extends Dialog {

    public final static int MENUTYPE_VERTICAL = 0;
    public final static int MENUTAPE_HORIZONTAL = 1;

    public interface OnViewItemSelectedListener {
        void onViewItemSelected(int position);
    }

    public interface OnButtonClickedListener {
        void onButtonClicked();
    }

    private OnViewItemSelectedListener mOnViewItemSelectedListener;
    private OnButtonClickedListener mOnButtonClickedListener;
    private MenuAdapter mMenuAdapter;

    private List<BottomSheetItem> mBottomSheetItems;
    private ViewGroup dialog;
    private GridView gd;
    private Activity context;
    private int menuType = MENUTYPE_VERTICAL;
    private String headerText = null;
    private String footerText = null;

    private TextView mHeader;
    private Button mFooter;
    private int numColumnsWhenVertical = 1;
    private int numColumnsWhenHorizontal = 4;


    public BottomSheet(Activity context, int menuType) {
        super(context, R.style.MaterialDialogSheet);
        this.context = context;
        this.menuType = menuType;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(true);
        getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.BOTTOM);
        dialog = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.bottom_sheet_menu, null);
        gd = (GridView) dialog.findViewById(R.id.gv);
    }

    public void init() {

        if (dialog != null) {
            bindHeaderAndFooter(dialog);
            gd.setAdapter(mMenuAdapter);
            if (menuType == MENUTYPE_VERTICAL) {
                gd.setNumColumns(numColumnsWhenVertical);
            } else {
                gd.setNumColumns(numColumnsWhenHorizontal);
            }
            gd.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    dismiss();
                    mOnViewItemSelectedListener.onViewItemSelected(position);
                }
            });
            mFooter.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnButtonClickedListener.onButtonClicked();
                }
            });
        } else {
            dialog = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.bottom_sheet_menu, null);
        }
        setContentView(dialog);

    }

    public void setAdapter(MenuAdapter mMenuAdapter) {
        if (mMenuAdapter != null) {
            this.mMenuAdapter = mMenuAdapter;
        }
    }

    public void setOnViewItemSelectedListener(OnViewItemSelectedListener mOnViewItemSelectedListener) {
        if (mOnViewItemSelectedListener != null) {
            this.mOnViewItemSelectedListener = mOnViewItemSelectedListener;
        }
    }

    public void setOnButtonClickedListener(OnButtonClickedListener mOnButtonClickedListener) {
        if (mOnButtonClickedListener != null) {
            this.mOnButtonClickedListener = mOnButtonClickedListener;
        }
    }

    public BottomSheet setHeaderText(String headerText) {
        this.headerText = headerText;
        return this;
    }

    public BottomSheet setFooterText(String footerText) {
        this.footerText = footerText;
        return this;
    }

    public BottomSheet setNumColumnsWhenVertical(int numColumns) {
        numColumnsWhenVertical = numColumns;
        return this;
    }

    public BottomSheet setNumColumnsWhenHorizontal(int numColumns) {
        numColumnsWhenHorizontal = numColumns;
        return this;
    }


    protected void bindHeaderAndFooter(View dialog) {

        mHeader = (TextView) dialog.findViewById(R.id.popup_menu_header);
        mFooter = (Button) dialog.findViewById(R.id.popup_menu_footer);

        if (headerText != null) {
            mHeader.setText(headerText);
        } else {
            mHeader.setVisibility(View.GONE);
        }
        if (footerText != null) {
            mFooter.setText(footerText);
        } else {
            mFooter.setVisibility(View.GONE);
        }
    }

}
